<template>
	<view class="consumer-detail">
		<uni-nav-bar class="uni-nav-bar" color="#ffffff" left-icon="left" @clickLeft="togo" title="充值" :border="false"
			fixed statusBar></uni-nav-bar>
		<view class="consumer-detail__topbox">
			<view class="balance">¥{{ Number(userInfo.balance).toFixed(2) }}</view>
			<view class="cash-btn">我的余额(余额)</view>
		</view>
		<view class="consumer-detail__list">
			<view class="title">充值金额</view>
			<view class="price"><text>￥</text> <input type="digit" v-model="num" placeholder="请输入充值金额"></view>
			<view class="desc">预计2小时内到账</view>
		</view>
		<view class="btn" @click="send">确认充值</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				loadMore: true,
				pageNum: 1,
				pageSize: 20,
				listData: [], // 精选推荐列表数据

				num: null
			}
		},
		onLoad() {
			this.getList()
		},
		computed: {
			...mapState(['userInfo'])
		},
		// 下拉刷新
		onPullDownRefresh() {
			uni.showNavigationBarLoading()
			this.refresh()
			uni.hideNavigationBarLoading()
			uni.stopPullDownRefresh()
		},
		// 上拉加载更多
		onReachBottom() {
			if (this.loadMore) {
				this.pageNum = this.pageNum + 1
				this.getList()
			}
		},
		methods: {
			// 获取
			refresh() {
				this.loadMore = true
				this.listData = []
				this.pageNum = 1
				this.getList()
			},
			// 获取明细列表
			async getList() {
				const data = await this.$apis.user.getBalance({
					pageNum: this.pageNum,
					pageSize: this.pageSize
				})
				this.listData = [...this.listData, ...data.rows]
				if (this.listData.length >= data.total) {
					this.loadMore = false
				}
			},
			togo() {
				uni.navigateBack(-1)
			},
			send() {
				if (!this.num) return uni.showToast({
					title: '请输入金额',
					icon: 'none'
				})
				this.$apis.paymemberlimit.rechargeBalance({
					amount: this.num
				}).then(res => {
					console.log(res)
					this.$apis.order.getWxpay({
						code: res.data.code,
						payType: 2,
					}).then(paydata => {
						console.log(paydata)
						uni.requestPayment({
							timeStamp: paydata.data.timeStamp,
							nonceStr: paydata.data.nonceStr,
							package: paydata.data.package,
							signType: paydata.data.signType,
							paySign: paydata.data.sign,
							success() {
								uni.showToast({
									title: '充值成功',
									icon: 'none',
									success() {
										setTimeout(() => {
											uni.navigateBack(-1)
										}, 1500)
									}
								})
							},
							fail() {
								uni.showToast({
									title: '取消了充值',
									icon: 'none'
								})
							}
						})
					})
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.consumer-detail {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-direction: column;
		min-height: 100%;
		background-color: #F8F8F8;

		.consumer-detail__topbox {
			width: 750rpx;
			height: 292rpx;
			background-image: linear-gradient(to right, #1EB75E, #2FCC71);
			display: flex;
			flex-direction: column;
			align-items: center;

			.balance {
				font-size: 90rpx;
				font-weight: bold;
				color: #FFFFFF;
				margin-top: 16rpx;
			}

			.cash-btn {
				width: 216rpx;
				height: 64rpx;
				margin-top: 8rpx;
				font-size: 30rpx;
				color: rgba(255, 255, 255, .8);
				font-weight: 300;
			}
		}

		.consumer-detail__list {
			width: 702rpx;
			height: 324rpx;
			background: #FFFFFF;
			border-radius: 10rpx;
			margin-top: -75rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex-direction: column;
			box-sizing: border-box;
			padding: 32rpx;

			.title {
				width: 100%;
				color: #333333;
				font-size: 28rpx;
				font-weight: 700;
				margin-bottom: 32rpx;
			}

			.price {
				width: 100%;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 116rpx;
				color: #333333;
				font-weight: 700;
				height: 84rpx;
				border-bottom: 2rpx solid #E8E8E8;

				text {
					color: #2FCC71;
					font-size: 50rpx;
				}

				input {
					flex: 1;
				}
			}

			.desc {
				color: #999999;
				font-size: 28rpx;
				width: 100%;
				margin-top: 32rpx;
			}

		}

		.btn {
			width: 686rpx;
			background-color: #2FCC71;
			color: #FFFFFF;
			margin-top: 72rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 96rpx;
			border-radius: 96rpx;
		}

	}
</style>
<style lang="scss">
	.uni-navbar__content {
		background-image: linear-gradient(to right, #1EB75E, #2FCC71);
	}

	label {
		display: flex;
		align-items: center;
	}

	.uni-navbar__header {
		background-color: transparent !important;
	}

	.uni-nav-bar-text {
		font-size: 32rpx !important;
	}
</style>